﻿using System.Collections.Generic;
using System.Linq;

namespace Sidvall.AuthorizationManager.UI.Entities
{
	public partial class AuthorizationScopeCollection
	{
		#region Business Members

		#region LoadAuthorizationScopeUsers

		public void LoadAuthorizationScopeUsers(AuthorizationScopeUserCollection items, bool setSelected, bool clear)
		{
			IEnumerable<AuthorizationScopeUser> filteredItems;
			AuthorizationScopeUser authorizationScopeUser;

			foreach (var item in this)
			{
				if ((clear) || (item.AuthorizationScopeUsers == null))
					item.AuthorizationScopeUsers = new AuthorizationScopeUserCollection();
				if (items != null)
				{
					filteredItems = (from o in items
									 where o.AuthorizationScopeId == item.AuthorizationScopeId
									 select o);
					foreach (var filteredItem in filteredItems)
					{
						authorizationScopeUser = filteredItem.Copy(Sidvall.Data.CopySettings.ItemCopy(Sidvall.Data.DataRowContextOption.State, null));
						item.AuthorizationScopeUsers.Add(authorizationScopeUser);
					}
				}
				if (setSelected)
					item.IsSelected = item.HasUser;
			}
		}

		#endregion
		#region SetUserId

		public void SetUserId(string userId)
		{
			foreach (var item in this)
				item.SetUserId(userId);
		}

		#endregion

		#endregion
	}
}